package com.ln.property_control.controller;

import com.ln.property_control.pojo.*;
import com.ln.property_control.service.TransferService;
import com.ln.property_control.utils.GetId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 刘念
 * @create 2021-07-08-15:48
 */
@RestController
@RequestMapping("/transfer")
public class TransferController {

    @Autowired
    private TransferService transferService;


    @RequestMapping("/selectTransferAll")
    public Map<String,Object> selectTransferAll(Integer page,Integer limit){
        return transferService.selectTransferAll(page,limit);
    }

    @RequestMapping("/selectByParentId")
    public List<Department> selectByParentId(int did){
        return transferService.selectByParentId(did);
    }


    @RequestMapping("/selectTransferType")
    public List<TransferType> selectTransferType(){
        return  transferService.selectTransferType();
    }

    @RequestMapping("/selectPropertyByDid")
    public List<PropertyVo> selectPropertyByDid(int did){
        return transferService.selectPropertyByDid(did);
    }

    @RequestMapping("/add")
    public int add(String[] property_id,HttpServletRequest request){
        String transfer_order_no = "ZY"+GetId.getTime().replaceAll("[[\\s-:punct:]]", "");
        int transfer_type = Integer.parseInt(request.getParameter("transfer_type"));
        String create_time = request.getParameter("create_time");
        String before_organization = request.getParameter("before_organization");
        String before_department = request.getParameter("before_department");
        String before_person = request.getParameter("before_person");
        String after_organization = request.getParameter("after_organization");
        String after_department = request.getParameter("after_department");
        String after_person = request.getParameter("after_person");
        String remarks = request.getParameter("remarks");

        Transfer transfer = new Transfer();
        transfer.setTransfer_order_no(transfer_order_no);
        transfer.setTransfer_type(transfer_type);
        transfer.setCreate_time(create_time);
        transfer.setBefore_organization(before_organization);
        transfer.setBefore_department(before_department);
        transfer.setBefore_person(before_person);

        transfer.setAfter_organization(after_organization);
        transfer.setAfter_department(after_department);
        transfer.setAfter_person(after_person);

        transfer.setRemarks(remarks);

        int depar_id = Integer.parseInt(request.getParameter("depar_id"));
        int after_depar_id = Integer.parseInt(request.getParameter("after_depar_id"));
        System.out.println(after_depar_id);

        int count = transferService.add(transfer, property_id,after_person,transfer_order_no,depar_id,after_depar_id);
        return count;
    }

    @RequestMapping("/updateStatus")
    public int updateStatus(String transfer_order_no,String user_name,String status,HttpSession session){
        int count = 0;
        User user = (User) session.getAttribute("userInfo");
        if(status.equals("转移终止")){
            if(user.getRankNum()==2){
                count = transferService.updateStatus(1, transfer_order_no,user_name);
                return 10004;
            }
            return 10001;
        }
        if(status.equals("已转移") || status.equals("转移终止")){
            return 10001;
        }else {
            if(status.equals("转移中")){
                if(user.getRankNum() == 1){
                    count = transferService.updateStatus(2, transfer_order_no,user_name);
                    return 10003;
                }
                if(user.getRankNum()==2){
                    count = transferService.updateStatus(1, transfer_order_no,user_name);
                    return 10004;
                }
            }
            if (status.equals("部门经理审批完成")){
                if(user.getRankNum() == 2){
                    count = transferService.updateStatus(1, transfer_order_no,user_name);
                    return 10004;
                }
                return 10002;
            }
        }
        return count;
    }

    @RequestMapping("/updateRefuseStatus")
    public int updateRefuseStatus(String transfer_order_no,String user_name,String status,HttpSession session){
        int count = 0;
        User user = (User) session.getAttribute("userInfo");
        if(status.equals("转移终止") || status.equals("已转移")){
            if(user.getRankNum() == 2){
                count = transferService.updateStatus(-1, transfer_order_no,user_name);
                return 10003;
            }
            return 10001;
        }
        if(status.equals("转移中")){
            if(user.getRankNum() == 1){
                count = transferService.updateStatus(-1, transfer_order_no,user_name);
                return 10003;
            }
            if(user.getRankNum() == 2){
                count = transferService.updateStatus(-1, transfer_order_no,user_name);
                return 10003;
            }
        }
        if (status.equals("部门经理审批完成")){
            if(user.getRankNum() == 2){
                count = transferService.updateStatus(-1, transfer_order_no,user_name);
                return 10004;
            }
            return 10002;
        }
        return count;
    }

    @RequestMapping("/selectAudit")
    public Map<String,Object> selectAudit(){
        return transferService.selectAudit();
    }
}
